barGraphDialog <-
function (){
	initializeDialog(title = gettext("Bar graph"))
	variableBox <- variableListBox(top, title = gettext("Variable (pick one)",domain="R-RcmdrPlugin.TeachingExtras"))
	.groups <- FALSE
	onOK <- function() {
		variable <- getSelection(variableBox)
		closeDialog()
		if (length(variable) == 0) {
			errorCondition(recall = barGraphDialog, message = gettext("You must select a variable.",domain="R-RcmdrPlugin.TeachingExtras"))
			return()
		}
		title <- tclvalue(titleVariable)
		leg <- tclvalue(legendVariable) == "1"
		rel <- tclvalue(relVariable) == "1"
        cum <- tclvalue(cumVariable) == "1"
        poly <- tclvalue(polyVariable) == "1"
		besid <- tclvalue(typeVariable) == "beside"
		if (tclvalue(coloVariable) == 1) {
			if (.groups == FALSE) 
				colcomm <- paste(", col=rainbow(length(table(", ActiveDataSet(), "$", variable, "))))", sep = "")
			else 
				colcomm <- paste(", col=rainbow(length(table(", ActiveDataSet(), "$", .groups, "))))", sep = "")
		}
		else {
			colcomm <- ", col=NULL)"
		}
		if (cum) { #Frecuencias acumuladas
			if (rel) {  #Frecuencias relativas
				if (poly) #Con polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=TRUE, rel=TRUE, poly=TRUE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
				else  # Sin polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=TRUE, rel=TRUE, poly=FALSE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
			}
			else { #Frecuencias absolutas
				if (poly) #Con polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=TRUE, rel=FALSE, poly=TRUE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
				else  # Sin polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=TRUE, rel=FALSE, poly=FALSE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
			}
		}
		else { #Frecuencias sin acumular
			if (rel) {  #Frecuencias relativas
				if (poly) #Con polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=FALSE, rel=TRUE, poly=TRUE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
				else  # Sin polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=FALSE, rel=TRUE, poly=FALSE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
			}
			else { #Frecuencias absolutas
				if (poly) #Con polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=FALSE, rel=FALSE, poly=TRUE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
				else  # Sin polígono
					command <- paste("barGraph(", ActiveDataSet(), "$", variable, ", cum=FALSE, rel=FALSE, poly=FALSE, main=\"", title, "\", xlab=\"", variable, "\", col=\"orange\", space=1)", sep = "")
			}
		}
		doItAndPrint(command)
		activateMenus()
		tkfocus(CommanderWindow())
	}
	groupsBox(barGraphDialog)
	OKCancelHelp(helpSubject = "barplot")
	radioButtons(name = "type", buttons = c("segmented", "beside"), 
			labels = gettext(c("Stacked bars", "Side-by-side bars"),domain="R-RcmdrPlugin.TeachingExtras"), 
			title = gettext("Display groups with:",domain="R-RcmdrPlugin.TeachingExtras"))
	optionsFrame <- tkframe(top)
	legendVariable <- tclVar("0")
	legendCheckBox <- tkcheckbutton(optionsFrame, variable = legendVariable)
	relVariable <- tclVar("0")
	relCheckBox <- tkcheckbutton(optionsFrame, variable = relVariable)
	cumVariable <- tclVar("0")
	cumCheckBox <- tkcheckbutton(optionsFrame, variable = cumVariable)
	polyVariable <- tclVar("0")
	polyCheckBox <- tkcheckbutton(optionsFrame, variable = polyVariable)
	coloVariable <- tclVar("0")
	coloCheckBox <- tkcheckbutton(optionsFrame, variable = coloVariable)
	titleFrame <- tkframe(top)
	titleVariable <- tclVar(gettext(""))
	titleField <- tkentry(titleFrame, width = "40", textvariable = titleVariable)
	tkgrid(tklabel(titleFrame, text = gettext("Title: ",domain="R-RcmdrPlugin.TeachingExtras"), fg = "blue"), titleField, sticky = "w")
	tkgrid(titleFrame, sticky = "w")
	tkgrid(getFrame(variableBox), sticky = "nw")
	tkgrid(tklabel(top, text = gettext("Options: ",domain="R-RcmdrPlugin.TeachingExtras"), fg = "blue"), sticky = "w")
	tkgrid(tklabel(optionsFrame, text = gettext("Relative frequencies ",domain="R-RcmdrPlugin.TeachingExtras"), justify = "left"), relCheckBox, 
		   tklabel(optionsFrame, text = gettext("Cumulative frequencies ",domain="R-RcmdrPlugin.TeachingExtras"), justify = "left"), cumCheckBox, sticky = "w")
    tkgrid(tklabel(optionsFrame, text = gettext("Polygon",domain="R-RcmdrPlugin.TeachingExtras"), justify = "left"), polyCheckBox, sticky = "w")
	tkgrid(tklabel(optionsFrame, text = gettext("Colored bars ",domain="R-RcmdrPlugin.TeachingExtras"), justify = "left"), coloCheckBox,
	       tklabel(optionsFrame, text = gettext("Leyend ",domain="R-RcmdrPlugin.TeachingExtras"), justify = "left"), legendCheckBox, sticky = "w")
	tkgrid(optionsFrame, sticky = "w")
	tkgrid(groupsFrame, sticky = "w")
	tkgrid(typeFrame, sticky = "w")
	tkgrid(buttonsFrame, sticky = "w")
	dialogSuffix(rows = 2, columns = 1)
}

